﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using System.Text;

public partial class online : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IMessageService m_MessageService;
    private HttpHelper m_HttpHelper = new HttpHelper();
    private WebMailService m_WebMailService = new WebMailService();

    private string m_FinishUrl = "online.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_MessageService = m_PostFactory.GetMessageService();

        if (!IsPostBack)
        {
            //清單
            Repeater_Bind();
        }
    }

    private void Repeater_Bind()
    {
        //搜尋條件
        string sortField = "CreatedDate";
        bool sortDesc = true;

        //分頁
        AspNetPager1.RecordCount = m_MessageService.CountMessage(true);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        Repeater1.DataSource = m_MessageService.GetMessageList(true, pageIndex, pageSize, sortField, sortDesc);
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Repeater_Bind();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //if (e.Item.ItemIndex != -1)
        //{
        //    Control ctrl = e.Item;
        //    MessageVO messageVO = (MessageVO)e.Item.DataItem;            
        //}
    }

    protected void btnSend_Click(object sender, ImageClickEventArgs e)
    {
        MessageVO messageVO = new MessageVO();
        UIHelper.FillVO(UpdatePanel1, messageVO);
        //手機
        if (!string.IsNullOrEmpty(txtMobile1_1.Text.Trim()) && !string.IsNullOrEmpty(txtMobile1_2.Text.Trim()))
        {
            messageVO.Mobile = txtMobile1_1.Text.Trim() + "-" + txtMobile1_2.Text.Trim();
        }
        //是否公開
        if (rdoUserAgreePublishY.Checked)
        {
            messageVO.UserAgreePublish = true;
        }
        //日期與IP
        messageVO.CreatedDate = DateTime.Now;
        messageVO.CreateIP = m_HttpHelper.GetUserIp(HttpContext.Current);

        //判斷必填與錯誤
        if (CheckErrorOK(messageVO))
        {
            m_MessageService.CreateMessage(messageVO);
            //發信
            m_WebMailService.SendMail_ToContactor_ByMessage(messageVO);
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("已成功送出諮詢，將儘速回覆您。感謝您的詢問。", m_FinishUrl), false);
        }
    }

    private bool CheckErrorOK(MessageVO messageVO)
    {
        bool result = true;
        StringBuilder sbError = new StringBuilder();

        //姓名必填
        if (string.IsNullOrEmpty(messageVO.CreateName))
        {
            sbError.Append("請輸入您的姓名。" + "\\n");
        }
        //電子信箱或行動電話必填
        if (string.IsNullOrEmpty(messageVO.EMail) && string.IsNullOrEmpty(messageVO.Mobile))
        {
            sbError.Append("請輸入您的電子信箱或是行動電話。" + "\\n");
        }
        //主旨必填
        if (string.IsNullOrEmpty(messageVO.Title))
        {
            sbError.Append("請輸入您的問題主旨。" + "\\n");
        }
        //主旨必填
        if (string.IsNullOrEmpty(messageVO.Content))
        {
            sbError.Append("請輸入您的問題內容。" + "\\n");
        }
        
        if (!string.IsNullOrEmpty(sbError.ToString()))
        {
            result = false;
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS(sbError.ToString()), false);
        }

        return result;
    }
}